package com.moliku.insurance.process.third;

import com.alibaba.fastjson.JSONObject;
import com.moliku.insurance.common.util.JacksonUtils;
import com.moliku.insurance.model.dto.InsuranceBaseParam;
import com.moliku.insurance.model.dto.pingan.token.PingAnTokenParam;
import com.moliku.insurance.model.vo.InsuranceBaseListVo;
import com.moliku.insurance.model.vo.InsuranceBaseVo;
import com.moliku.insurance.model.vo.pingan.token.PingAnTokenVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 平安第三方调用
 * @author way
 * @date 2021-03-06
 */
@Slf4j
@Component
public class PinAnThirdApi extends BaseThirdApi{

    /**
     *带token调用
     *@param baseUrl 基础路径
     *@param    t 请求
     *@return
     *@throws
     *@author way
     *@date 2021/03/05 13:10
     */
    public <R> InsuranceBaseVo<R> executeWithToken(String baseUrl, InsuranceBaseParam<R> t){

        String url=baseUrl+t.url();
        url=String.format("%s?access_token=%s",url,getToken());

        Map<String, Object> params= JacksonUtils.objToMap(t);
        String ret= InsuranceBaseParam.METHOD.GET.equals(t.methodType())?get(url,params):post(url,params);
        log.error("远程调用 。请求url:{},请求参数:{} .返回结果:{}",url, JSONObject.toJSON(params),ret);
        return toResponse(ret,t.responseType());
    }

    /**
     *带token调用
     *@param baseUrl 基础路径
     *@param    t 请求
     *@return
     *@throws
     *@author way
     *@date 2021/03/05 13:10
     */
    public <R> InsuranceBaseListVo<R> executeListWithToken(String baseUrl, InsuranceBaseParam<R> t){

        String url=baseUrl+t.url();
        url=String.format("%s?access_token=%s",url,getToken());

        Map<String, Object> params= JacksonUtils.objToMap(t);
        String ret= InsuranceBaseParam.METHOD.GET.equals(t.methodType())?get(url,params):post(url,params);
        log.error("远程调用 。请求url:{},请求参数:{} .返回结果:{}",url, JSONObject.toJSON(params),ret);
        return toResponses(ret,t.responseType());
    }

    /**
     *获取token
     *@return
     *@throws
     *@author way
     *@date 2021/03/05 13:10
     */
    private String getToken(){
        PingAnTokenParam param=new PingAnTokenParam();
        InsuranceBaseVo<PingAnTokenVo> response=execute("https://test-api.pingan.com.cn:20443",param);
        return response.getData().getAccessToken();
    }


    /**
     *消息转换为Response
     *@param responseStr 返回数据
     *@return
     *@throws
     *@author way
     *@date 2021/03/05 13:10
     */
    @Override
    public <T> InsuranceBaseVo<T> toResponse(String responseStr, Class<T> clazz) {
        InsuranceBaseVo response= JacksonUtils.jsonToObj(responseStr,InsuranceBaseVo.class);
        response.setData(JacksonUtils.jsonToObj(JacksonUtils.objToJson(response.getData()),clazz));
        if(null==response){
            log.error("远程调用格式化返回为空.responseStr:{}",responseStr);
            return null;
        }
        return response;
    }

    /**
     *消息转换为Response
     *@param responseStr 返回数据
     *@return
     *@throws
     *@author way
     *@date 2021/03/05 13:10
     */
    public <T> InsuranceBaseListVo<T> toResponses(String responseStr, Class<T> clazz) {
        if(StringUtils.isBlank(responseStr)){
            return null;
        }
        responseStr=responseStr.replaceAll("\"\\[","[");
        responseStr=responseStr.replaceAll("\\]\"","]");
        InsuranceBaseListVo<T> response=JacksonUtils.jsonToObj(responseStr,InsuranceBaseListVo.class);

        response.setData(JacksonUtils.jsonToObjs(JacksonUtils.objToJson(response.getData()),clazz));
        if(null==response){
            log.error("远程调用格式化返回为空.responseStr:{}",responseStr);
            return null;
        }

        return response;
    }

}
